/**
  ******************************************************************************
  * @file    IAP/inc/common.h 
  * @author  MCD Application Team
  * @version V3.3.0
  * @date    10/15/2010
  * @brief   This file provides all the headers of the common functions.
  ******************************************************************************
  * @copy
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
  */ 

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef _COMMON_H
#define _COMMON_H

/* Includes ------------------------------------------------------------------*/
#include "stdio.h"
#include "string.h"
#include "gd32f4xx.h"
#include "ymodem.h"

/* Exported types ------------------------------------------------------------*/
typedef  void (*pFunction)(void);

/* Exported constants --------------------------------------------------------*/

/*	GD32F407VET6  系统FLASH扇区分配:
扇区0 0x0800 0000 - 0x0800 3FFF	   16KB			BootLoader	16KB
扇区1 0x0800 4000 - 0x0800 7FFF    16KB			FLAG				16KB
扇区2 0x0800 8000 - 0x0800 BFFF    16KB			APP
扇区3 0x0800 C000 - 0x0800 FFFF    16KB			APP
扇区4 0x0801 0000 - 0x0801 FFFF    64KB			APP
扇区5 0x0802 0000 - 0x0803 FFFF    128KB		APP					224KB
扇区6 0x0804 0000 - 0x0805 FFFF    128KB		None
扇区7 0x0806 0000 – 0x0807 FFFF    128KB		None
*/

/* Constants used by Serial Command Line Mode */
#define CMD_STRING_SIZE       128


#define FLAG_ENTER_APP    0xAA55AA55  // 进入APP的标志（32位）
#define FLAG_ENTER_BOOT   0x55AA55AA  // 进入Bootloader的标志

/* 烧录APP标志位起始扇区地址,不可与APP或者BootLoader处于同一个扇区 */
#define BOOT_FLAG_ADDR 						((uint32_t)0x08004000U)
/* APP程序起始地址 */
#define ApplicationAddress        ((uint32_t)0x08008000U)
/* APP程序结束地址 */
#define ApplicationEndAddress     ((uint32_t)0x0803FFFFU)
/* 具体如何分配看个人,注意查看用户手册,同时注意需要在keil的配置里进行同步修改 */

/* 这里请根据对应不同型号的芯片填写,是总FLASH大小
 * 由于GD32F407VET6不支持flash的page操作,所以这里就没有写PAGE_SIZE */
#define FLASH_SIZE                        (0x80000)  /* 512KBytes */

/* Compute the FLASH upload image size */  
#define FLASH_IMAGE_SIZE                   (uint32_t) (FLASH_SIZE - (ApplicationAddress - 0x08000000))

/* Exported macro ------------------------------------------------------------*/
/* Common routines */
#define IS_AF(c)  ((c >= 'A') && (c <= 'F'))
#define IS_af(c)  ((c >= 'a') && (c <= 'f'))
#define IS_09(c)  ((c >= '0') && (c <= '9'))
#define ISVALIDHEX(c)  IS_AF(c) || IS_af(c) || IS_09(c)
#define ISVALIDDEC(c)  IS_09(c)
#define CONVERTDEC(c)  (c - '0')

#define CONVERTHEX_alpha(c)  (IS_AF(c) ? (c - 'A'+10) : (c - 'a'+10))
#define CONVERTHEX(c)   (IS_09(c) ? (c - '0') : CONVERTHEX_alpha(c))

#define SerialPutString(x) Serial_PutString((uint8_t*)(x))

/* Exported functions ------------------------------------------------------- */
void Int2Str(uint8_t* str,int32_t intnum);
uint32_t Str2Int(uint8_t *inputstr,int32_t *intnum);
uint32_t GetIntegerInput(int32_t * num);
uint32_t SerialKeyPressed(uint8_t *key);
uint8_t GetKey(void);
void SerialPutChar(uint8_t c);
void Serial_PutString(uint8_t *s);
void GetInputString(uint8_t * buffP);
void FLASH_DisableWriteProtectionPages(void);
void Main_Menu(void);
void SerialDownload(void);
void SerialUpload(void);
void Jump_to_APP_Program(void);
#endif  /* _COMMON_H */

/*******************(C)COPYRIGHT 2010 STMicroelectronics *****END OF FILE******/
